using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;
public partial class Admin_DonHangList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    private void LoadData()
    {
        PagerV2_8_1 .PageSize = 5;
        int total = 0;
        Criteria cri = new Criteria();
        if (txt_IdDH.Text != "")
        {
            cri.add(Condition.AND, tbDonHangColumns.IdDH.ToString(), Condition.EQUAL, int.Parse (txt_IdDH.Text));
        }
        Repeater1.DataSource = tbDonHangBUS.Get(cri.Criter, PagerV2_8_1.CurrentIndex,
            PagerV2_8_1.PageSize, tbDonHangColumns.NgayDatHang .ToString(), OrderDirection.DESC.ToString(), out total);
        Repeater1.DataBind();
        PagerV2_8_1.ItemCount = total;
        if (PagerV2_8_1.ItemCount <= PagerV2_8_1.PageSize)
            PagerV2_8_1.Visible = false;
        else
            PagerV2_8_1.Visible = true;
    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        PagerV2_8_1.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
            e.Item.ItemType == ListItemType.Item)
        {
            tbDonHang donHang = (tbDonHang)e.Item.DataItem;
            tbKhachHang khachHang = tbKhachHangBUS.GetByID(donHang.IdDH );
            if (khachHang != null)
            {
                Literal lit_KhachHang = (Literal)e.Item.FindControl("lit_KhachHang");
                lit_KhachHang.Text = khachHang.HoTen;
            }
            HyperLink hyp_DonHang = (HyperLink)e.Item.FindControl("hyp_DonHang");
            hyp_DonHang.Text = donHang.IdDH.ToString();
            hyp_DonHang.NavigateUrl = "~/Admin/CTDonHangList.aspx?id=" + donHang.IdDH.ToString();

            CheckBox cb_DaGiao = (CheckBox)e.Item.FindControl("cb_DaGiao");
            tbTrangThaiDH trangthaidh = tbTrangThaiDHBUS.GetById(int.Parse(donHang.IdTrangThaiDH.ToString()));
            cb_DaGiao.Checked = trangthaidh .TrangThai_DH ;
            if (cb_DaGiao.Checked == true)
            {
                cb_DaGiao.Enabled = false;
            }

            HiddenField hf_ID = (HiddenField)e.Item.FindControl("hf_ID");
            hf_ID.Value = donHang.IdDH .ToString();
        }
    }
    protected void btn_Update_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_DaGiao = (CheckBox)Repeater1.Items[i].FindControl("cb_DaGiao");
            if (cb_DaGiao.Enabled == true && cb_DaGiao.Checked == true)
            {
                HiddenField hf_ID = (HiddenField)Repeater1.Items[i].FindControl("hf_ID");
                tbDonHang donHang = tbDonHangBUS.GetByID(int.Parse(hf_ID.Value));
                donHang.IdTrangThaiDH = 1 ;
                tbDonHangBUS.Update(donHang);
            }
        }
        Response.Redirect(Request.RawUrl);
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        LoadData();
    }
}
